package com.zr.userlogin.service.impl;



import com.zr.userlogin.mapper.UserMapper;
import com.zr.userlogin.pojo.LoginVo1;
import com.zr.userlogin.pojo.UserLogin;
import com.zr.userlogin.pojo.registerVo;
import com.zr.userlogin.service.UserService;
import com.zr.util.ResultVOBuilder;
import com.zr.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by 86151 on 2019/6/14.
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     */
    @Override
    @Transactional
    public ResultVo register(registerVo registerVo) {
        //逻辑验证
        ResultVo assigment = assigment(registerVo);
        if (!assigment.isSuccess()){
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        UserLogin userLogin = new UserLogin();
        Date nowDate = new Date();
        userLogin.setCreateTime(nowDate);
        userLogin.setCreateName("小明");
        userLogin.setUpdateTime(nowDate);
        userLogin.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(registerVo,userLogin);
        userMapper.register(registerVo);
        return ResultVOBuilder.success(registerVo);
    }

    /**
     * 登录
     */
    @Override
    public ResultVo login(LoginVo1 loginVo1, HttpServletRequest request) {
        if (loginVo1.getIphone() == null){
            ResultVOBuilder.error("登录时id不能为空","500");
        }
        if (loginVo1.getVerificationCode() == null){
            ResultVOBuilder.error("登录时验证码不能为空","500");
        }
        UserLogin userLogin = userMapper.login(loginVo1);
        if (userLogin == null){
            ResultVOBuilder.error("登录时对象不存在","500");
        }
        request.getSession().setAttribute("loginId", userLogin.getId());
        return ResultVOBuilder.success(userLogin);
    }

    @Override
    public ResultVo inputInvitationCode(String invitationCode) {
        UserLogin inputInvitationCode = userMapper.inputInvitationCode(invitationCode);
        if (inputInvitationCode == null){
            return ResultVOBuilder.error("邀请码不能为空","500");
        }

        return ResultVOBuilder.success(invitationCode);
    }

    public ResultVo assigment(registerVo registerVo){
        //密码和确定密码是否一致
        if (!registerVo.getUserPwd().equals(registerVo.getConfirmPwd())){
            return ResultVOBuilder.error("密码和确认密码不一致","500");
        }
        //        1.验证手机号是否已经存在
        List<UserLogin> queryUserByPhone = userMapper.queryUserByPhone(registerVo.getIphone());
        if (!CollectionUtils.isEmpty(queryUserByPhone)){
            return ResultVOBuilder.error("该手机号已经注册过了","500");
        }

        return ResultVOBuilder.success(registerVo);
    }

}
